package com.zggk.lzz.bridget;

import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zggk.lzz.R;
import com.zggk.lzz.callback.WeiboDialogUtils;
import com.zggk.lzz.mvp.MVPBaseActivity;
import com.zggk.lzz.roadt.XzBean;
import com.github.mikephil.charting.charts.HorizontalBarChart;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.IValueFormatter;
import com.github.mikephil.charting.utils.ViewPortHandler;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class BridgeTActivity extends MVPBaseActivity<BridgeTContract.View, BridgeTPresenter> implements BridgeTContract.View {


    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.title_text)
    TextView titleText;
    @Bind(R.id.cancle_button)
    TextView cancleButton;
    @Bind(R.id.xz_chart)
    PieChart xzChart;
    @Bind(R.id.xz_d_lay)
    LinearLayout xzDLay;
    @Bind(R.id.xz_bar)
    HorizontalBarChart xzBar;
    @Bind(R.id.js_chart)
    PieChart jsChart;
    @Bind(R.id.js_d_lay)
    LinearLayout jsDLay;
    @Bind(R.id.js_bar)
    HorizontalBarChart jsBar;
    @Bind(R.id.disease_new_parent_layout)
    LinearLayout diseaseNewParentLayout;
    private Dialog mWeiboDialog;

    @Override
    public void onRequestError(String msg) {

    }

    @Override
    protected int getLayout() {
        return R.layout.act_bridge_t;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        setTitleText("桥梁信息统计");
        goBack();
        initData();
    }

    private void initData() {
        mWeiboDialog = WeiboDialogUtils.createLoadingDialog(BridgeTActivity.this, "加载中...");
        mPresenter.getJSData();
        mPresenter.getKJData();
    }

    @Override
    public void getJSDatas(List<XzBean> jsBeanList) {
        List<String> xzBarDataDW = new ArrayList<>();
        List<BarEntry> xzBarData = new ArrayList<>();
        List<PieEntry> xzPieData = new ArrayList<>();
        for (int i = 0; i < jsBeanList.size(); i++) {
            xzBarDataDW.add(getName(jsBeanList.get(i).getRoad_type()));
            xzBarData.add(new BarEntry(i, Float.valueOf(jsBeanList.get(i).getRoad_length())));
        }
        for (int i = 0; i < jsBeanList.size(); i++) {
            if (!jsBeanList.get(i).getRoad_type().equals("HJ")) {
                xzPieData.add(new PieEntry(Float.valueOf(jsBeanList.get(i).getRoad_length()), ""));
            }
        }
        setXzChart(xzChart, xzPieData);
        setJsChart(xzBar, xzBarDataDW, xzBarData);
        WeiboDialogUtils.closeDialog(mWeiboDialog);
    }

    @Override
    public void getKJDatas(List<XzBean> kjBeanList) {
        List<String> xzBarDataDW = new ArrayList<>();
        List<BarEntry> xzBarData = new ArrayList<>();
        List<PieEntry> xzPieData = new ArrayList<>();
        for (int i = 0; i < kjBeanList.size(); i++) {
            xzBarDataDW.add(getName2(kjBeanList.get(i).getRoad_type()));
            xzBarData.add(new BarEntry(i, Float.valueOf(kjBeanList.get(i).getRoad_length())));
        }
        for (int i = 0; i < kjBeanList.size(); i++) {
            if (!kjBeanList.get(i).getRoad_type().equals("HJ")) {
                xzPieData.add(new PieEntry(Float.valueOf(kjBeanList.get(i).getRoad_length()), ""));
            }
        }
        setXzChart(jsChart, xzPieData);
        setJsChart(jsBar, xzBarDataDW, xzBarData);
        WeiboDialogUtils.closeDialog(mWeiboDialog);
    }

    private String getName(String road_type) {
        if (road_type.equals("HJ")) {
            return "合计座数";
        } else if (road_type.equals("YL")) {
            return "一类桥梁";
        } else if (road_type.equals("EL")) {
            return "二类桥梁";
        } else if (road_type.equals("SL")) {
            return "三类桥梁";
        } else if (road_type.equals("SIL")) {
            return "四类桥梁";
        } else if (road_type.equals("WL")) {
            return "五类桥梁";
        }
        return "合计里程";
    }

    private String getName2(String road_type) {
        if (road_type.equals("HJ")) {
            return "合计座数";
        } else if (road_type.equals("TD")) {
            return "特大桥";
        } else if (road_type.equals("DQ")) {
            return "大桥";
        } else if (road_type.equals("ZQ")) {
            return "中桥";
        } else if (road_type.equals("XQ")) {
            return "小桥";
        }
        return "合计座数";
    }

    private void setXzChart(PieChart xz_chart, List<PieEntry> xzPieData) {
        PieDataSet dataSet = new PieDataSet(xzPieData, "");
        ArrayList<Integer> colors = new ArrayList<Integer>();
        colors.add(getResources().getColor(R.color.one_color));
        colors.add(getResources().getColor(R.color.two_color));
        colors.add(getResources().getColor(R.color.three_color));
        colors.add(getResources().getColor(R.color.four_color));
        colors.add(getResources().getColor(R.color.five_color));
        dataSet.setColors(colors);
        PieData pieData = new PieData(dataSet);
        pieData.setDrawValues(true);
        xz_chart.setData(pieData);
        xz_chart.invalidate();
        Description description = new Description();
        description.setText("");
        xz_chart.setDescription(description);
        xz_chart.setTransparentCircleRadius(0f);
        xz_chart.setHoleRadius(30f);
        pieData.setDrawValues(false);
        Legend legend = xz_chart.getLegend();//设置比例图
        legend.setEnabled(false);//图例不显示
        xz_chart.invalidate();
    }

    private void setJsChart(HorizontalBarChart xz_bar, final List<String> xzBarDataDW, List<BarEntry> xzBarData) {
        xz_bar.getDescription().setEnabled(false); // 不显示描述
        xz_bar.getLegend().setEnabled(false); // 不显示图例
        xz_bar.setExtraOffsets(0, 0, 15, 0);  // 设置饼图的偏移量，类似于内边距 ，设置视图窗口大小
        XAxis xAxis = xz_bar.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(false);
        xAxis.setAxisLineColor(Color.WHITE);
        xAxis.setTextSize(12f);
        xAxis.setLabelCount(5);
        xAxis.setGranularity(1f); // 防止放大图后，标签错乱
        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float v, AxisBase axisBase) {
                try {
                    return xzBarDataDW.get((int) v);
                } catch (Exception e) {
                    return "";
                }
            }
        });

        YAxis yAxis_right = xz_bar.getAxisRight();
        yAxis_right.setEnabled(false);
        YAxis yAxis_left = xz_bar.getAxisLeft();
        yAxis_left.setAxisMinimum(0f);
        yAxis_left.setAxisMaximum(3000f);
        yAxis_left.setEnabled(false);
        BarDataSet barDataSet = new BarDataSet(xzBarData, "");
        ArrayList<Integer> colors = new ArrayList<Integer>();
        colors.add(getResources().getColor(R.color.five_color));
        colors.add(getResources().getColor(R.color.one_color));
        colors.add(getResources().getColor(R.color.two_color));
        colors.add(getResources().getColor(R.color.three_color));
        colors.add(getResources().getColor(R.color.four_color));
        colors.add(getResources().getColor(R.color.six_color));
        barDataSet.setColors(colors);
        barDataSet.setValueTextColor(Color.BLACK);
        barDataSet.setValueTextSize(12f);
        barDataSet.setValueFormatter(new IValueFormatter() {
            @Override
            public String getFormattedValue(float v, Entry entry, int i, ViewPortHandler viewPortHandler) {
                return v + "座";
            }
        });
        BarData barData = new BarData(barDataSet);
        barData.setBarWidth(0.2f); // 设置柱子的宽度
        xz_bar.setData(barData);
        xz_bar.invalidate();
    }
}